/*
 *  WaveTableWithArray.h
 *  SoundController0002
 *
 *  Created by jiro on 10/11/06.
 *  Copyright 2010 jiro music. All rights reserved.
 *
 */

#include <vector>
#include "WaveTable.h"
#include "AllExceptions.h"

namespace sc
{
	class WaveTableWithArray : public WaveTable
	{
	private:
	
		const std::vector< double >& values;

	public:

		WaveTableWithArray( const std::vector< double >& values, double adjuster )
		  : WaveTable( WaveTable::Type_Array, -1, adjuster ),
		    values( values )
		{}

		bool operator == ( const WaveTableWithArray& rhs ) const
		{
			if ( *static_cast< const WaveTable* >( this ) != 
				 static_cast< const WaveTable& >( rhs ) ) {
				return false;
			}
		
			size_t count = values.size();
			if ( count != rhs.values.size() ) {
				return false;
			}
			
			for ( int i = 0; i < count; i++ ) {
				if ( values[ i ] != rhs.values[ i ] ) {
					return false;
				}
			}
			return true;
		}

		bool operator != ( const WaveTableWithArray& rhs ) const
		{
			return ! ( *this == rhs );
		}

		//	for std::set
		virtual bool operator < ( const WaveTableWithArray& rhs ) const 
		{
			throw NotImplementedException( __FUNCTION__ );
			return false;
		}

		double operator [] ( double time ) const 
		{
			size_t count = values.size();
			while ( time > count - 1 ) {
				time -= count;
			}

			int index0 = static_cast<int>( time );
			int index1 = index0 + 1;
			if ( index1 > count ) {
				index1 -= 1;
			}

			double rate0 = 1.0 - ( time - static_cast<int>( time ) );
			double rate1 = 1.0 - rate0;

			return WaveTable::adjuster * ( rate0 * values.at( index0 ) + rate1 * values.at( index1 ) );
		}

		const std::vector< double >& GetArray() const
		{
			return values;
		}
	};

}